package com.js.nickcar.module.home;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.api.UserHelper;
import com.js.nickcar.base.BaseFragment;
import com.js.nickcar.dialog.DialogHelper;
import com.js.nickcar.module.addcar.baseinfo.BaseCarInfoFragment;
import com.js.nickcar.module.login.LoginFragment;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by LZY on 2017/9/28.
 */

public class HomeFragment extends BaseFragment<HomePresenter> implements HomeView {

    @Bind(R.id.imageBtn_add_car)
    ImageButton mImageBtnAddCar;
    @Bind(R.id.tv_exit_account)
    TextView mTvExitAccount;
    public AlertDialog mExitDialog;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }

    public static HomeFragment getInstance() {
        return new HomeFragment();
    }

    @Override
    protected void initData() {
    }


    @OnClick({R.id.imageBtn_add_car, R.id.tv_exit_account})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageBtn_add_car:
                openAddBaseCarnfoFragment();
                break;
            case R.id.tv_exit_account:
                exitAccount();
                break;
        }

    }

    private void exitAccount() {
        mExitDialog = DialogHelper.showConfirmDialog(getActivity(), "是否退出登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                UserHelper.exit();
                replaceFragment(LoginFragment.getInstance());
            }
        }).create();
        mExitDialog.show();
    }

    private void openAddBaseCarnfoFragment() {
        addFragment(BaseCarInfoFragment.getInstance());
    }

    @Override
    public void onPause() {
        super.onPause();
        DialogHelper.closeDialog(mExitDialog);
    }
}
